/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Dsrv6.persistence.controllers;

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import Dsrv6.persistence.entities.Register;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import Dsrv6.persistence.controllers.exceptions.NonexistentEntityException;
import Dsrv6.persistence.entities.IdType;

/**
 *
 * @author a381u296
 */
public class IdTypeJpaController implements Serializable {

    public IdTypeJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(IdType idType) {
        if (idType.getRegisterList() == null) {
            idType.setRegisterList(new ArrayList<Register>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<Register> attachedRegisterList = new ArrayList<Register>();
            for (Register registerListRegisterToAttach : idType.getRegisterList()) {
                registerListRegisterToAttach = em.getReference(registerListRegisterToAttach.getClass(), registerListRegisterToAttach.getRegId());
                attachedRegisterList.add(registerListRegisterToAttach);
            }
            idType.setRegisterList(attachedRegisterList);
            em.persist(idType);
            for (Register registerListRegister : idType.getRegisterList()) {
                IdType oldIdtIdtIdOfRegisterListRegister = registerListRegister.getIdtIdtId();
                registerListRegister.setIdtIdtId(idType);
                registerListRegister = em.merge(registerListRegister);
                if (oldIdtIdtIdOfRegisterListRegister != null) {
                    oldIdtIdtIdOfRegisterListRegister.getRegisterList().remove(registerListRegister);
                    oldIdtIdtIdOfRegisterListRegister = em.merge(oldIdtIdtIdOfRegisterListRegister);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(IdType idType) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            IdType persistentIdType = em.find(IdType.class, idType.getIdtId());
            List<Register> registerListOld = persistentIdType.getRegisterList();
            List<Register> registerListNew = idType.getRegisterList();
            List<Register> attachedRegisterListNew = new ArrayList<Register>();
            for (Register registerListNewRegisterToAttach : registerListNew) {
                registerListNewRegisterToAttach = em.getReference(registerListNewRegisterToAttach.getClass(), registerListNewRegisterToAttach.getRegId());
                attachedRegisterListNew.add(registerListNewRegisterToAttach);
            }
            registerListNew = attachedRegisterListNew;
            idType.setRegisterList(registerListNew);
            idType = em.merge(idType);
            for (Register registerListOldRegister : registerListOld) {
                if (!registerListNew.contains(registerListOldRegister)) {
                    registerListOldRegister.setIdtIdtId(null);
                    registerListOldRegister = em.merge(registerListOldRegister);
                }
            }
            for (Register registerListNewRegister : registerListNew) {
                if (!registerListOld.contains(registerListNewRegister)) {
                    IdType oldIdtIdtIdOfRegisterListNewRegister = registerListNewRegister.getIdtIdtId();
                    registerListNewRegister.setIdtIdtId(idType);
                    registerListNewRegister = em.merge(registerListNewRegister);
                    if (oldIdtIdtIdOfRegisterListNewRegister != null && !oldIdtIdtIdOfRegisterListNewRegister.equals(idType)) {
                        oldIdtIdtIdOfRegisterListNewRegister.getRegisterList().remove(registerListNewRegister);
                        oldIdtIdtIdOfRegisterListNewRegister = em.merge(oldIdtIdtIdOfRegisterListNewRegister);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = idType.getIdtId();
                if (findIdType(id) == null) {
                    throw new NonexistentEntityException("The idType with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            IdType idType;
            try {
                idType = em.getReference(IdType.class, id);
                idType.getIdtId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The idType with id " + id + " no longer exists.", enfe);
            }
            List<Register> registerList = idType.getRegisterList();
            for (Register registerListRegister : registerList) {
                registerListRegister.setIdtIdtId(null);
                registerListRegister = em.merge(registerListRegister);
            }
            em.remove(idType);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<IdType> findIdTypeEntities() {
        return findIdTypeEntities(true, -1, -1);
    }

    public List<IdType> findIdTypeEntities(int maxResults, int firstResult) {
        return findIdTypeEntities(false, maxResults, firstResult);
    }

    private List<IdType> findIdTypeEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(IdType.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public IdType findIdType(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(IdType.class, id);
        } finally {
            em.close();
        }
    }

    public int getIdTypeCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<IdType> rt = cq.from(IdType.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
